#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>


main()
{
  char fn[]="link.example.file";
  char ln[]="link.example.link";
  int file_descriptor;
  struct stat info;

  if ((file_descriptor = creat(fn, S_IWUSR)) < 0)
    perror("creat() error");
  else {
    close(file_descriptor);
    puts("before link()");
    stat(fn,&info);
    printf("   number of links is %hu\n",info.st_nlink);
    if (link(fn, ln) != 0) {
      perror("link() error");
      unlink(fn);
    }
    else {
      puts("after link()");
      stat(fn,&info);
      printf("   number of links is %hu\n",info.st_nlink);
      unlink(ln);
      puts("after first unlink()");
      stat(fn,&info);
      printf("   number of links is %hu\n",info.st_nlink);
      unlink(fn);
    }
  }
}

